package com.guo.xiao.bing.service;

import com.guo.xiao.bing.dao.JoinProjectMapper;
import com.guo.xiao.bing.dao.ProjectMapper;
import com.guo.xiao.bing.dao.ProjectTypeMapper;
import com.guo.xiao.bing.dao.UserMapper;
import com.guo.xiao.bing.entity.*;
import com.guo.xiao.bing.vo.JoinProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class JoinProjectService {

    @Autowired
    JoinProjectMapper joinProjectMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    ProjectMapper projectMapper;

    public List<JoinProjectVo> find() {
        List<JoinProject> joinProjects = joinProjectMapper.selectByExample(null);


        List<JoinProjectVo> joinProjectVos = E2Vo(joinProjects);
        return joinProjectVos;
    }

    public List<JoinProjectVo> findByUser(Integer userId) {
        JoinProjectExample example = new JoinProjectExample();
        example.createCriteria().andUserIdEqualTo(userId);
        List<JoinProject> joinProjects = joinProjectMapper.selectByExample(example);

        List<JoinProjectVo> joinProjectVos = E2Vo(joinProjects);
        return joinProjectVos;
    }

    public List<JoinProjectVo> find(Integer status) {
        JoinProjectExample example = new JoinProjectExample();
        example.createCriteria().andStatusEqualTo(3);
        List<JoinProject> joinProjects = joinProjectMapper.selectByExample(example);


        List<JoinProjectVo> joinProjectVos = E2Vo(joinProjects);
        return joinProjectVos;
    }

    public List<JoinProjectVo> findByUser(Integer status, Integer userId) {
        JoinProjectExample example = new JoinProjectExample();
        example.createCriteria().andUserIdEqualTo(userId).andStatusEqualTo(3);
        List<JoinProject> joinProjects = joinProjectMapper.selectByExample(example);

        List<JoinProjectVo> joinProjectVos = E2Vo(joinProjects);
        return joinProjectVos;
    }

    public List<JoinProjectVo> findByProject(Integer projectId) {
        JoinProjectExample example = new JoinProjectExample();
        example.createCriteria().andOrgIdEqualTo(projectId);
        List<JoinProject> joinProjects = joinProjectMapper.selectByExample(example);

        List<JoinProjectVo> joinProjectVos = E2Vo(joinProjects);
        return joinProjectVos;
    }

    private List<JoinProjectVo> E2Vo(List<JoinProject> joinProjects) {
        List<JoinProjectVo> joinProjectVos = new ArrayList<>();
        for (JoinProject j : joinProjects) {
            JoinProjectVo jpv = new JoinProjectVo();

            Project project = null;
            if (j.getOrgId() != 0) {
                project = projectMapper.selectByPrimaryKey(j.getOrgId());
            } else {
                project = new Project();
                project.setTitle("该志愿项目已被删除");
                project.setContent("该志愿项目已被删除");
            }

            User user = userMapper.selectByPrimaryKey(j.getUserId());

            jpv.setProject(project);
            jpv.setUser(user);
            jpv.setOrgId(j.getOrgId());
            jpv.setUserId(j.getUserId());
            jpv.setContent(j.getContent());
            jpv.setCreateAt(j.getCreateAt());
            jpv.setId(j.getId());
            jpv.setStatus(j.getStatus());

            joinProjectVos.add(jpv);
        }
        return joinProjectVos;
    }

    public JoinProject findById(Integer id) {
        return joinProjectMapper.selectByPrimaryKey(id);
    }

    public Integer save(JoinProject joinProject) {
        joinProject.setStatus(1);
        return joinProjectMapper.insert(joinProject);
    }

    public Integer update(JoinProject joinProject) {
        return joinProjectMapper.updateByPrimaryKeySelective(joinProject);
    }

    public Integer delete(Integer id) {
        return joinProjectMapper.deleteByPrimaryKey(id);
    }
}
